<!-- 该画面用于进行项目核算履历修改。 -->
<template>
  <el-dialog
    :visible.sync="openSheet"
    width="1200px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('projectMaintainName')">
            <el-input v-model="form.project_name" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('projectMaintainSalesDate')">
            <el-input v-model="form.sales_date" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('maintainAccountSalesAmount')">
            <el-input v-model="form.sales_amount" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('maintainAccountAmount')">
            <el-input v-model="form.amount" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('maintainAccountProfitLoss')">
            <el-input v-model="form.profit_loss" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('projectMaintainNote')" prop="note" :error="error.note">
            <el-input v-model="form.note" :placeholder="$t('projectMaintainNote')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">{{ $t('update') }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateMaintainAccountApi, updateDevelopAccountApi } from '../api/projectcombineaccount.api';

export default {
  data() {
    return {
      formRef: 'form',
      openSheet: false,
      updateTitle: this.$t('projectCombineAccountUpdate'),
      projectPlanId: 0,
      modelType: '',
      form: {
        projectPlanFK: undefined,
        note: undefined
      },
      error: {
        projectPlanFK: undefined,
        note: undefined
      },
      rules: {
      }
    };
  },
  computed: {
    labelWidth() {
      return this.$i18n.locale === 'en' ? '150px' : '110px';
    }
  },
  methods: {
    openSheetDialog(accountdata) {
      this.openSheet = !this.openSheet;
      this.reset();
      this.initialize();
      if (accountdata) {
        this.title = this.updateTitle;
        this.modelType = accountdata.model_type;
        this.form = { ...accountdata };
        this.accountid = accountdata.foreign_key_id;
      }
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form);
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    initialize() {
    },
    submitForm: function() {
      this.$refs['form'].validate(async(Valid) => {
        if (Valid) {
          this.resetJsonObject(this.error);
          var Resp;
          if (this.modelType === 'D') {
            Resp = await updateDevelopAccountApi(this.accountid, this.form);
          } else {
            Resp = await updateMaintainAccountApi(this.accountid, this.form);
          }
          if (Resp.success) {
            this.$emit('refreshList');
            this.cancel();
            this.targaMessage(
              this.$t('updateSuccess', {
                title: this.$t('projectMaintainName'),
                value: this.form.project_name
              })
            );
          } else {
            if (typeof Resp.msg === 'object') {
              this.error = Resp.msg;
            } else {
              this.targaMessageBox(this, Resp.msg);
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped>

</style>
